import React, { PureComponent, Fragment } from "react"
import {
  Table,
  Input,
  InputNumber,
  Popconfirm,
  Form,
  Button,
  Divider,
  Tooltip,
  Select
} from "antd"
import isEqual from "lodash/isEqual"
import { connect } from "dva"
import { formatMessage } from "umi/locale"

const FormItem = Form.Item
const EditableContext = React.createContext()

const EditableRow = ({ form, index, ...props }) => (
  <EditableContext.Provider value={form}>
    <tr {...props} />
  </EditableContext.Provider>
)

const EditableFormRow = Form.create()(EditableRow)

class EditableCell extends React.Component {
  getInput = () => {
    return <Input />
  }

  render() {
    const {
      editing,
      dataIndex,
      title,
      inputType,
      record,
      index,
      max,
      min,
      step,
      ...restProps
    } = this.props
    return (
      <EditableContext.Consumer>
        {(form) => {
          const { getFieldDecorator } = form
          return (
            <td {...restProps}>
              {editing ? (
                <FormItem style={{ margin: 0 }}>
                  {getFieldDecorator(dataIndex, {
                    rules: [
                      {
                        required: true,
                        message:
                          formatMessage({ id: "app.tools.pleaseInput" }) +
                          `${title}!`
                      }
                    ],
                    initialValue: record[dataIndex]
                  })(this.getInput())}
                </FormItem>
              ) : (
                restProps.children
              )}
            </td>
          )
        }}
      </EditableContext.Consumer>
    )
  }
}

@connect(({ es, hd }) => ({ es, hd }))
class PcDataEmp extends React.Component {
  constructor(props) {
    super(props)
    this.state = { editingKey: "" }
    this.columns = [
      {
        title: "ID",
        dataIndex: "ID",
        editable: true,
        width: "14.25%",
        inputType: "string",
        key: "1"
      },
      {
        title: "员工工号",
        dataIndex: "EMPNO",
        editable: true,
        width: "14.25%",
        inputType: "string",
        key: "2"
      },
      {
        title: "员工姓名",
        dataIndex: "EMPNAME",
        editable: true,
        width: "14.25%",
        inputType: "string",
        key: "3"
      },
      {
        title: "操作代码",
        dataIndex: "EMPCODE",
        editable: true,
        width: "14.25%",
        inputType: "string",
        key: "4"
      },
      {
        title: "操作名称",
        dataIndex: "EMPDES",
        editable: true,
        width: "14.25%",
        inputType: "string",
        key: "5"
      },
      {
        title: "操作时间",
        dataIndex: "EMPTIME",
        editable: true,
        width: "14.25%",
        inputType: "string",
        key: "6"
      },
      {
        title: "时间",
        dataIndex: "CREATETIME",
        editable: true,
        width: "14.25%",
        inputType: "string",
        key: "7"
      }
    ]
  }
  isEditing = (record) => record.key === this.state.editingKey

  edit(key) {
    this.setState({ editingKey: key })
  }

  render() {
    const { serverPcDataEmp } = this.props.es
    const components = {
      body: {
        row: EditableFormRow,
        cell: EditableCell
      }
    }

    const columns = this.columns.map((col) => {
      if (!col.editable) {
        return col
      }
      return {
        ...col,
        onCell: (record) => ({
          record,
          inputType: col.inputType,
          dataIndex: col.dataIndex,
          max: col.max,
          min: col.min,
          step: col.step,
          title: col.title,
          editing: this.isEditing(record)
        })
      }
    })

    return (
      <Fragment>
        <Table
          rowKey={(record, index) => index}
          bordered
          components={components}
          dataSource={serverPcDataEmp}
          columns={columns}
          rowClassName="editable-row"
          size={"small"}
          pagination={{ pageSize: 8 }}
        />
      </Fragment>
    )
  }
}

export default PcDataEmp
